import Vue from 'vue'
import VueRouter from "vue-router";
import Admin from "@/pages/admin/Admin";
import UserLayout from "@/pages/UserLayout/UserLayout";
import store from "@/vuex/store";
import {Message} from 'ant-design-vue';
import {getUserInfo} from "@/api/user";
import {excludePathPatterns} from "@/config/router_premission";

Vue.use(VueRouter)
const originalPush = VueRouter.prototype.push
VueRouter.prototype.push = function push(location) {
    return originalPush.call(this, location).catch(err => err)
}
const originalReplace = VueRouter.prototype.replace
VueRouter.prototype.replace = function replace(location) {
    return originalReplace.call(this, location).catch(err => err)
}

/** router.js */
const router = new VueRouter({
    mode: 'history',
    routes: [
        {
            path: '/user',
            component: UserLayout,
            redirect: '/user/login',
            children: [
                {
                    path: 'login',
                    name: 'login',
                    meta: {
                        isAuth: false,
                        path:'/user/login'
                    },
                    component: () => import(/* webpackChunkName: "user" */ '@/pages/Login')
                },
                {
                    path: 'register',
                    name: 'register',
                    meta: {
                        isAuth: false,
                        path:'/user/register'
                    },
                    component: () => import(/* webpackChunkName: "user" */ '@/pages/Register')
                },
                {
                    path: 'register-result',
                    name: 'registerResult',
                    meta: {
                        isAuth: false,
                        path:'/user/register-result'
                    },
                    component: () => import(/* webpackChunkName: "user" */ '@/pages/RegisterResult')
                },
                {
                    path: 'recover',
                    name: 'recover',
                    meta: {
                        isAuth: true,
                        path:'/user/recover'
                    },
                    component: undefined
                }
            ]
        },
        {
            path: '/admin',
            name: 'admin',
            component: Admin,
            children: [
                {
                    path: 'index',
                    name: 'index',
                    meta: {
                        isAuth: true,
                        path:'/admin/index'
                    },
                    component: () => import(/* webpackChunkName: "user" */ '@/pages/home/index')
                },
                {
                    path: 'managerUser',
                    name: 'managerUser',
                    meta: {
                        isAuth: true,
                        path:'/admin/managerUser'
                    },
                    component: () => import(/* webpackChunkName: "user" */ '@/pages/system/users')
                },
                {
                    path: 'myinfo',
                    name: 'myinfo',
                    meta: {
                        isAuth: true,
                        path:'/admin/myinfo'
                    },
                    component: () => import(/* webpackChunkName: "user" */ '@/pages/home/center/Myinfo')
                },
                {
                    path: 'userinfo/:userId',
                    name: 'userinfo',
                    meta: {
                        isAuth: false,
                    },
                    component: () => import(/* webpackChunkName: "user" */ '@/pages/home/center/Userinfo')
                },
                {
                    path: "logs",
                    name: "logs",
                    meta: {
                        isAuth: true,
                        path:'/admin/logs'
                    },
                    component: () => import(/* webpackChunkName: "user" */ '@/pages/logs/LogInfo')
                },
                {
                    path: "settings",
                    name: "settings",
                    meta: {
                        isAuth: true,
                        path:'/admin/settings'
                    },
                    component: () => import(/* webpackChunkName: "user" */ '@/pages/home/settings/Settings')
                },
                {
                    path: "menuManager",
                    name: "menuManager",
                    meta: {
                        isAuth: true,
                        path:'/admin/menuManager'
                    },
                    component: () => import(/* webpackChunkName: "user" */ '@/pages/system/menu')
                },
                {
                    path: "roleManager",
                    name: "roleManager",
                    meta: {
                        isAuth: true,
                        path:'/admin/roleManager'
                    },
                    component: () => import(/* webpackChunkName: "user" */ '@/pages/system/role')
                },
                {
                    path: "notice",
                    name: "notice",
                    meta: {
                        isAuth: true,
                        path:'/admin/notice'
                    },
                    component: () => import(/* webpackChunkName: "user" */ '@/pages/system/notice')
                },
                {
                    path: "pushResource",
                    name: "pushResource",
                    meta: {
                        isAuth: true,
                        path:'/admin/pushResource'
                    },
                    component: () => import(/* webpackChunkName: "user" */ '@/pages/home/push')
                },
                {
                    path: "/blog/detail/:blogId",
                    name: "blogDetail",
                    meta: {
                        isAuth: false,
                    },
                    component: () => import(/* webpackChunkName: "user" */ '@/pages/home/blog/detail')
                },
                {
                    path: "/res/detail/:resId",
                    name: "resDetail",
                    meta: {
                        isAuth: false,
                    },
                    component: () => import(/* webpackChunkName: "user" */ '@/pages/home/res/Detail')
                },
                {
                    path: "dashboard",
                    name: "dashboard",
                    meta: {
                        isAuth: true,
                        path:'/admin/dashboard'
                    },
                    component: () => import(/* webpackChunkName: "user" */ '@/pages/dashboard/dashboard')
                },
                {
                    path: "managerComment",
                    name: "managerComment",
                    meta: {
                        isAuth: true,
                        path:'/admin/managerComment'
                    },
                    component: () => import(/* webpackChunkName: "user" */ '@/pages/comment/managerComment')
                },
                {
                    path: "montor",
                    name: "montor",
                    meta: {
                        isAuth: true,
                        path:'/admin/montor'
                    },
                    component: () => import(/* webpackChunkName: "user" */ '@/pages/system/montor')
                },
                {
                    path: "clearlogs",
                    name: "clearlogs",
                    meta: {
                        isAuth: true,
                        path:'/admin/clearlogs'
                    },
                    component: () => import(/* webpackChunkName: "user" */ '@/pages/logs/clearlogs')
                },
                {
                    path: 'report',
                    name: 'report',
                    meta: {
                        isAuth: true,
                        path:'/admin/report'
                    },
                    component: () => import(/* webpackChunkName: "user" */ '@/pages/report/report')
                },
                {
                    path: 'report/detail/:id',
                    name: 'reportDetail',
                    component: () => import(/* webpackChunkName: "user" */ '@/pages/report/detail')
                },
                {
                    path: 'pk/detail/:id',
                    name: 'pkDetail',
                    meta: {
                        path:'/admin/index'
                    },
                    component: () => import(/* webpackChunkName: "user" */ '@/pages/home/pk/detail')
                },
                {
                    path: 'otherManager',
                    name: 'otherManager',
                    meta: {
                        isAuth: true,
                        path:'/admin/otherManager'
                    },
                    component: () => import(/* webpackChunkName: "user" */ '@/pages/system/other')
                },
                {
                    path: 'annoCenter',
                    name: 'annoCenter',
                    meta: {
                        isAuth: true,
                        path:'/admin/annoCenter'
                    },
                    component: () => import(/* webpackChunkName: "user" */ '@/pages/system/annoCenter')
                },
                {
                    path: 'recycle',
                    name: 'recycle',
                    meta: {
                        isAuth: true,
                        path:'/admin/recycle'
                    },
                    component: () => import(/* webpackChunkName: "user" */ '@/pages/system/recycle')
                },
                {
                    path: 'contact',
                    name: 'contact',
                    meta: {
                        isAuth: true,
                        path:'/admin/contact'
                    },
                    component: () => import(/* webpackChunkName: "user" */ '@/pages/home/contact/contactus')
                },
                {
                    path: 'reportRun',
                    name: 'reportRun',
                    meta: {
                        isAuth: true,
                        path:'/admin/reportRun'
                    },
                    component: () => import(/* webpackChunkName: "user" */ '@/pages/home/progress/progress')
                },
                {
                    path: 'sysLogs',
                    name: 'sysLogs',
                    meta: {
                        isAuth: true,
                        path:'/admin/sysLogs'
                    },
                    component: () => import(/* webpackChunkName: "user" */ '@/pages/logs/sysLogs')
                },
                {
                    path: 'recycle/detail/:id',
                    name: 'recycleDetail',
                    meta: {
                        isAuth: false,
                        path:'/admin/recycle'
                    },
                    component: () => import(/* webpackChunkName: "user" */ '@/pages/system/recycle/detail')
                },
                {
                    path: '403',
                    meta: {
                        isAuth: false,
                    },
                    component: () => import(/* webpackChunkName: "user" */ '@/pages/exception/403'),
                },
                {
                    path: '*',
                    meta: {
                        isAuth: false,
                    },
                    component: () => import(/* webpackChunkName: "user" */ '@/pages/exception/404')
                }
            ],
            redirect: '/admin/index'
        },
        {
            path: '/500',
            meta: {
                isAuth: false,
            },
            component: () => import(/* webpackChunkName: "user" */ '@/pages/exception/500')
        },
        // 重定向路由
        {
            path: '/',
            meta: {
                isAuth: false,
            },
            redirect: '/user/login'
        },
        {
            path: '*',
            meta: {
                isAuth: false,
            },
            component: () => import(/* webpackChunkName: "user" */ '@/pages/exception/404')
        }
    ]
})

router.beforeEach(async (to, from, next) => {
    const toPath = to.path;
    // 白名单路由,未登录也能去
    if (excludePathPatterns.includes(toPath)){
        next();
    // 首先确认用户是否登录
    } else {
        let loginUser = store.state.user.loginUser || {};
        let routes = store.state.user.routes || [];
        if (loginUser.loginName === undefined) {
            // 不存在，从服务器获取
            const result = await getUserInfo();
            const {code, data} = result;
            if (code === 200) {
                // 用户信息获取成功
                store.dispatch('saveUser', data);
                const {routes} = data;
                if (to.meta.isAuth){
                    if (routes.indexOf(to.path) != -1) {
                        // 有权限
                        next();
                    } else {
                        // 无权限
                        next("/admin/403");
                    }
                } else{
                    next();
                }
            } else {
                // 获取用户信息失败
                Message.error("您的登录信息已过期,请重新登陆")
                store.dispatch("errorHandler")
                next('/user/login');
            }
        } else {
            if (to.meta.isAuth){
                if (routes.indexOf(to.path) != -1) {
                    // 有权限
                    next();
                } else {
                    // 无权限
                    next("/admin/403");
                }
            } else{
                next();
            }
        }
    }
})

export default router;
